package cn.aixmy.base.interceptor;

import cn.aixmy.base.vo.Result;
import cn.aixmy.utils.XmUtils;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 全局拦截返回值
 *
 * @author DaenMax
 */
@ControllerAdvice
public class XmResponseBodyAdvice implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (request.getURI().toString().contains("soft")){
            Result result = (Result) body;
            JSONObject retJson = JSONObject.from(result.getResult());
            if (retJson != null) {
                body = XmUtils.calculateSignReturn(result.getMsg(), retJson, 1, "asjdhsjsjahsjdjf");
            }
            return body;
        }
        return body;
    }
}
